package com.turnengine.client.global.admin.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import java.io.IOException;

/**
 * The Execute Script Data Serializer.
 */
public class ExecuteScriptDataSerializer extends AbstractCommandDataSerializer<Boolean> implements IExecuteScriptDataSerializer {

	/**
	 * Creates a new Execute Script Data Serializer.
	 */
	public ExecuteScriptDataSerializer() {
		super(ExecuteScript.COMMAND_ID);
	}

	@Override
	public Boolean readReturnValue(IDataReader reader) throws IOException {
		Boolean value = reader.readBooleanObject(true);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, Boolean value) throws IOException {
		writer.writeBooleanObject(value, true);
	}

	@Override
	public IExecutableCommand<Boolean> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		String name = reader.readString(true);

		// Create object
		ExecuteScript object = new ExecuteScript();
		object.setLoginId(loginId);
		object.setName(name);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<Boolean> argument) throws IOException {
		ExecuteScript object = (ExecuteScript) argument;

		// Get fields
		long loginId = object.getLoginId();
		String name = object.getName();

		// Write fields
		writer.writeLong(loginId);
		writer.writeString(name, true);
	}

	@Override
	public IExecutableCommandResponse<Boolean> newResponse() {
		return new ExecuteScriptResponse();
	}
}
